package com.shop.goods.controller;

import com.shop.domain.ProductDetails;
import com.shop.entity.Result;
import com.shop.goods.service.ProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品详情表
 */
@RestController
@RequestMapping("/ProductDetails")
public class ProductDetailsController {
    @Autowired
    private ProductDetailsService iProductDetailsService;


    /**
     * 查询全部商品详情表
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<ProductDetails>> selectAll(){
        return new Result<>("查询成功",iProductDetailsService.selectAll());
    }


    /**
     * 条件查询商品详情表
     * @param iProductDetails
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<ProductDetails> > select(@RequestBody ProductDetails iProductDetails){
        return new Result<>("条件查询成功",iProductDetailsService.selectList(iProductDetails));
    }

    /**
     * 新增商品详情表
     * @param iProductDetails
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody ProductDetails iProductDetails){
        iProductDetailsService.insert(iProductDetails);
        return new Result<>("新增成功");
    }


    /**
     * 删除商品详情表
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        iProductDetailsService.delete(ids);
        return new Result<>("删除成功");
    }


    /**
     * 修改商品详情表
     * @param iProductDetails
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody ProductDetails iProductDetails){
        iProductDetailsService.update(iProductDetails);
        return new Result<>("修改成功");
    }

    /**
     * 减库存
     * @param productId
     * @param num
     * @return
     */
    @GetMapping("/reduceStock")
    public Result reduceStock(@RequestParam Long productId, @RequestParam Integer num){
        Integer i = iProductDetailsService.reduceStock(productId, num);
        if(i!=1){
            return Result.fail("库存不足");
        }
        return Result.ok("减库存成功");
    }
}
